// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Author: wsfuyibing <682805@qq.com>
// Date: 2024-12-03

package app

type (
	// MessageStatus
	// is a type name use to remark received message status.
	MessageStatus     int
	MessageStatusText string
)

const (
	MessageStatusCreated MessageStatus = iota
	MessageStatusSucceed
	MessageStatusFailed
	MessageStatusWaiting
	MessageStatusProcessing
	MessageStatusFiltered
)

const (
	MessageStatusTextCreated    MessageStatusText = "created"
	MessageStatusTextSucceed    MessageStatusText = "succeed"
	MessageStatusTextFailed     MessageStatusText = "failed"
	MessageStatusTextWaiting    MessageStatusText = "waiting"
	MessageStatusTextProcessing MessageStatusText = "processing"
	MessageStatusTextFiltered   MessageStatusText = "filtered"
)

var (
	messageStatusText = map[MessageStatus]MessageStatusText{
		MessageStatusCreated:    MessageStatusTextCreated,
		MessageStatusSucceed:    MessageStatusTextSucceed,
		MessageStatusFailed:     MessageStatusTextFailed,
		MessageStatusWaiting:    MessageStatusTextWaiting,
		MessageStatusProcessing: MessageStatusTextProcessing,
		MessageStatusFiltered:   MessageStatusTextFiltered,
	}
	messageStatusTextUnknown StatusText = ""
)

func (o MessageStatus) Created() bool    { return o == MessageStatusCreated }
func (o MessageStatus) Failed() bool     { return o == MessageStatusFailed }
func (o MessageStatus) Filtered() bool   { return o == MessageStatusFiltered }
func (o MessageStatus) Processing() bool { return o == MessageStatusProcessing }
func (o MessageStatus) Succeed() bool    { return o == MessageStatusSucceed }
func (o MessageStatus) Waiting() bool    { return o == MessageStatusWaiting }

func (o MessageStatus) Text() string {
	if s, ok := messageStatusText[o]; ok {
		return s.String()
	}
	return messageStatusTextUnknown.String()
}

func (o MessageStatusText) Can() (status MessageStatus, has bool) {
	switch o {
	case MessageStatusTextCreated:
		return MessageStatusCreated, true
	case MessageStatusTextSucceed:
		return MessageStatusSucceed, true
	case MessageStatusTextFailed:
		return MessageStatusFailed, true
	case MessageStatusTextWaiting:
		return MessageStatusWaiting, true
	case MessageStatusTextProcessing:
		return MessageStatusProcessing, true
	case MessageStatusTextFiltered:
		return MessageStatusFailed, true
	}
	return
}

func (o MessageStatusText) String() string {
	return string(o)
}
